import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Welcome from '../views/Welcome.vue'
import UserList from '../views/UserList.vue'
import Rights from '../views/Rights.vue'
import Roles from '../views/Roles.vue'
import Cate from '../views/Cate.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      // { path: '/home', redirect: '/welcome' },
      { path: '/welcome', component: Welcome },
      { path: '/users', component: UserList },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  console.log(to.path, 'to.path')
  console.log(from.path, 'from.path')
  if (to.path !== '/login') {
    const token = sessionStorage.getItem('token')
    if (!token) return next('/login')
  }
  next()
})

export default router
